package com.probiz.estore.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.product.SkuOption;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.SkuOptionDao;
import com.probiz.estore.product.dao.SkuOptionValueDao;
import com.probiz.estore.product.model.SkuOptionFullyModel;
import com.probiz.estore.product.service.ProductSkuManager;
import com.probiz.estore.product.service.SkuOptionManager;
import com.probiz.estore.product.util.ProdCodeGenerator;


/**
 * Manager implementation for SkuOption, responsible for business processing, and communicate between web and persistence layer.
 */
public class SkuOptionManagerImpl extends GenericManagerImpl<SkuOption> implements SkuOptionManager {

    private SkuOptionDao skuOptionDao = null;
    private SkuOptionValueDao skuOptionValueDao = null;

    /**
     * @param skuOptionDao the skuOptionDao to set
     */
    public void setSkuOptionDao(SkuOptionDao skuOptionDao) {
        this.skuOptionDao = skuOptionDao;
    }

    public void setSkuOptionValueDao(SkuOptionValueDao skuOptionValueDao) {
        this.skuOptionValueDao = skuOptionValueDao;
    }

    /*
    * (non-Javadoc)
    *
    * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
    */
    @Override
    protected void initManager() {
        dao = skuOptionDao;
    }

    /*
      * (non-Javadoc)
      *
      * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
      */
    @Override
    protected void preDelete(SkuOption entity) {

    }

    /*
      * (non-Javadoc)
      *
      * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
      */
    @Override
    protected void preSave(SkuOption entity) {

    }


    public List<SkuOption> findSkuOptionsByProductType(Integer productTypeId) {
        return skuOptionDao.findSkuOptionsByProductType(productTypeId);
    }

    public List<SkuOption> findActiveSkuOptionsByProductType(
            Integer productTypeId) {
        return skuOptionDao.findActiveSkuOptionsByProductType(productTypeId);
    }


    public List<SkuOption> findActiveSkuOptionsExcludeRefProductType(Integer productTypeId) {
        List<SkuOption> allActiveSkuOptions = skuOptionDao.listByProperty("status", Constants.STATUS_ACTIVE);
        List<SkuOption> productTypeActiveSkuOptions = findActiveSkuOptionsByProductType(productTypeId);
        //清除与该产品类型关联的Sku属性选项
        for (SkuOption skuOption : productTypeActiveSkuOptions) {
            if (allActiveSkuOptions.contains(skuOption)) {
                allActiveSkuOptions.remove(skuOption);
            }
        }
        return allActiveSkuOptions;
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.SkuOptionManager#getSkuOptionByCode(java.lang.String)
     */
    public SkuOption getSkuOptionByCode(String skuOptionCode) {
        SkuOption skuOption = dao.getUniqueByProperty("skuOptionCode", skuOptionCode);
        return skuOption;
    }

    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.SkuOptionManager#saveFully(com.probiz.estore.product.model.SkuOptionFullyModel)
     */
    public Map<String, Object> saveFully(SkuOptionFullyModel model) {
        Map<String, Object> returnMsgs = new HashMap<String, Object>();
        SkuOption skuOption = model.getSkuOption();
        if(skuOption.getSkuOptionId() == null)skuOption.setSkuOptionCode(ProdCodeGenerator.generateSkuOptionCode());
		skuOptionDao.save(skuOption);
        List<String> newSkuOptionValueIds = skuOptionValueDao.saveSkuOptionValuesFromArrays(
                skuOption, model.getSkuOptionValueIds(),model.getSkuOptionValueNames(), model.getSkuOptionValueTypes(), model.getSkuOptionValueValues());
        returnMsgs.put("newSkuOptionValueIds", newSkuOptionValueIds);
        return returnMsgs;

    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.SkuOptionManager#deleteFully(java.lang.Integer)
     */
    public short deleteFully(Integer skuOptionId){
    	Long count = skuOptionDao.getSizeOfProductSkuOptionValue(skuOptionId);
		if(count.longValue() == (long)0){
			skuOptionDao.delete(skuOptionDao.getById(skuOptionId));
			return DELETE_STATUS_TRUE;
		}else{
			return DELETE_STATUS_FALSE;
		}
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.SkuOptionManager#getNumOfQuickSearchOptionsByProductTypeId(java.lang.Integer)
     */
    public Long getNumOfQuickSearchOptionsByProductTypeId(Integer productTypeId){
    	return skuOptionDao.getNumOfQuickSearchOptionsByProductTypeId(productTypeId);
    }
   /*
    * (non-Javadoc)
    * @see com.probiz.estore.product.service.SkuOptionManager#getQuickSearchSkuOptionsByProductTypeId(java.lang.Integer)
    */
    public List<SkuOption> getQuickSearchSkuOptionsByProductTypeId(Integer productTypeId){
    	return skuOptionDao.getQuickSearchSkuOptionsByProductTypeId(productTypeId);
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.SkuOptionManager#getSizeOfProductSkuOptionValueByProductTypeAndSkuOption(java.lang.Integer, java.lang.Integer)
     */
    public Long getSizeOfProductSkuOptionValueByProductTypeAndSkuOption(Integer productTypeId,Integer skuOptionId){
    	return skuOptionDao.getSizeOfProductSkuOptionValueByProductTypeAndSkuOption(productTypeId, skuOptionId);
    }
    
    


}
